import React from 'react'
// ===================================================================== Temp
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import Confirm from '#cpt/temp/Confirm'
import SearchForm from '#cpt/temp/SearchForm'
// ===================================================================== global declare
const { $Lazy, $http, $fn } = window
// ===================================================================== Lazy
const $cache = $Lazy.file(()=>import('#private/cache-api'))
const Box = $Lazy.load(()=>import('#tp/box/box'))
const Add = $Lazy.state(()=>import('./cpt/add-modal'))
const Allow = $Lazy.state(()=>import('./cpt/allow-modal'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		forms: [
			{ label: '标本类型',		name: 'spec_type',	type: 'select', loading:true, idStr:'code' },
		]
	}
	
	model = {}
	
	componentDidMount(){
		const { kind_id } = this.props
		this.kind_id = kind_id 
		this.fetch()
	}
	
	// paging
	fetch = () => {
		$http.submit(this,'collection/disspeclist', {param:{kind_id: this.kind_id, spec_type: this.model?.spec_type, page: 1}, loadingText:'数据加载中'}).then((data)=>{
			this.setState({data})
		})
	}
	
	// 获取下拉框数据
	getSelect = async () => {
		const { forms } = this.state
		$cache.then( async f => {
			//标本类型
			$fn.setRow(forms, 'spec_type', await f.getDicCache({ code: 2000 }) )
			//
			this.setState({ forms })
		})
	}
	
	// 表单
	cols = [
		{ title: '标本类型', 	dataIndex: 'item_name' }
	]
	
	// 操作按钮
	controls = () => {
		const { selectedKeys } = this.state
		return [
			Controls.click.call(this, '添加', () => {
				const { kind_id } = this.props
				this.modal.open(kind_id)
			}),
			Confirm.call(this,'删除', 'collection/deldisspectype', { param: () => ({item_code: selectedKeys, kind_id: this.kind_id}), isKeys: true, ghost:true})
		]
	}
	render(){
		const { data } = this.state
		const keys = []
		data.forEach(v => {
			keys.push(v.item_code)
		})
		return (
			<>
				<div className="fx">
					<Box className='ex mr5' title="禁用标本类型" controls={this.controls()}>
						{/* 搜索 */}
						{SearchForm.call(this, { onForm: this.getSelect, noRefresh: true })}
						{/* 表格 */}
						{ Table.call(this, { idStr: 'item_code', wrapStyle: {height: 500} }) }
						<Add type={1} keys={keys} onRef={ref=>this.modal=ref} onRefresh={this.fetch} />
					</Box>
					<Allow kind_id={this.kind_id} tableStyle={this.props.tableStyle} />
				</div>
			</>
		)
	}
}

export default Index